package com.boge.modules.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boge.common.utils.ShiroUtils;
import com.boge.modules.house.entity.THouseEntity;
import com.boge.modules.house.entity.TLeaseEntity;
import com.boge.modules.house.service.THouseService;
import com.boge.modules.house.service.TLeaseService;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boge.common.utils.PageUtils;
import com.boge.common.utils.Query;

import com.boge.modules.house.dao.TReservationDao;
import com.boge.modules.house.entity.TReservationEntity;
import com.boge.modules.house.service.TReservationService;


@Service("tReservationService")
public class TReservationServiceImpl extends ServiceImpl<TReservationDao, TReservationEntity> implements TReservationService {

    @Autowired
    private THouseService houseService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private TLeaseService leaseService;

    /**
     * 查询预订信息
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SysUserEntity loginUser = ShiroUtils.getUserEntity();
        long userId = loginUser.getUserId();
        // 根据当前登录用户 查询出对应的所有的房屋信息
        List<THouseEntity> list = houseService.list(new LambdaQueryWrapper<THouseEntity>().eq(THouseEntity::getUserId, userId));
        List<Integer> houseIds = new ArrayList<>();
        if(list != null ){
            for (THouseEntity tHouseEntity : list) {
                houseIds.add(tHouseEntity.getId());
            }
        }
        IPage<TReservationEntity> page = this.page(
                new Query<TReservationEntity>().getPage(params),
                new LambdaQueryWrapper<TReservationEntity>()
                        .in(!"admin".equals(loginUser.getUsername()) && houseIds.size() !=0 ,TReservationEntity::getHouseId,houseIds)

        );

        List<TReservationEntity> records = page.getRecords();
        if(records != null){
            for (TReservationEntity record : records) {
                userId = record.getUserId();
                SysUserEntity userEntity = userService.getById(userId);
                record.setUserName(userEntity.getNickname());
                Integer houseId = record.getHouseId();
                THouseEntity houseEntity = houseService.getById(houseId);
                record.setHouseName(houseEntity.getName());
                record.setHouseImg(WebController.BASE_URL+houseEntity.getImg());
                // 判断当前的用户和房屋是否存在 签合同的情况
                List<TLeaseEntity> leases = leaseService.list(new LambdaQueryWrapper<TLeaseEntity>().eq(TLeaseEntity::getHouseId, record.getHouseId())
                        .eq(TLeaseEntity::getUserId, record.getUserId()));
                if(leases != null && leases.size() > 0){
                    record.setStatus(1);
                }else{
                    // 还未签合同
                    record.setStatus(0);
                }
            }
        }

        return new PageUtils(page);
    }

}